package neusoft.yuejian_11.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import neusoft.yuejian_11.base.BaseActivity;
import neusoft.yuejian_11.bean.GetInfo;
import neusoft.yuejian_11.bean.IsTrainn;
import neusoft.yuejian_11.bean.TrainBean;
import neusoft.yuejian_11.bean.TrainnBean;
import neusoft.yuejian_11.listener.RetrofitListener;
import neusoft.yuejian_11.model.TrainModel;
import neusoft.yuejian_11.model.UserModel;
import neusoft.yuejian_11.R;

public class TrainnActivity extends BaseActivity implements RetrofitListener {
    protected Context context;
    private int trainId;
    private int userId;
    private int clubId;
    private ImageButton imageButtonBack;
    private TextView textTrainName;
    private TextView textTrainType;
    private TextView textTrainPrice;
    private TextView textUserName;
    private TextView textUserPhone;
    private Button buttonTrainn;
    private ImageButton imageButtonFile;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_trainn);

        initViews();//初始化
        initEvents();//点击事件处理
        showData();//传参
        initData();//网络获取
    }

    private void initViews() {
        imageButtonBack = (ImageButton) findViewById(R.id.imageButtom_back);
        textTrainName = (TextView) findViewById(R.id.text_train_name);
        textTrainType = (TextView) findViewById(R.id.text_train_type);
        textTrainPrice = (TextView) findViewById(R.id.text_train_price);
        textUserName = (TextView) findViewById(R.id.text_user_name);
        textUserPhone = (TextView) findViewById(R.id.text_user_phone);
        buttonTrainn = (Button) findViewById(R.id.button_trainn);
        imageButtonFile = (ImageButton) findViewById(R.id.imageButtom_file);
    }

    private void initEvents() {
        imageButtonFile.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(TrainnActivity.this, InformationActivity.class);
                intent.putExtra("t_id",trainId);
                startActivity(intent);
            }
        });

        imageButtonBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        buttonTrainn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                TrainModel trainModel = new TrainModel();
                trainModel.trainn(getU_id(),trainId,TrainnActivity.this);
            }
        });

    }

    private void showData() {
        trainId = getIntent().getIntExtra("t_id",-1);
    }

    private void initData() {
        TrainModel trainModel1 = new TrainModel();
        trainModel1.trainDetail(String.valueOf(trainId),this);

        TrainModel trainModel=new TrainModel();
        trainModel.IsAddTrainn(getU_id(),String.valueOf(trainId),this);

        UserModel userModel = new UserModel();
        userModel.getUserInfo(getU_id(),this);
    }

    @Override
    public void onSuccess(Object o, int flag) {
        if (flag==1) {
            TrainBean trainBean = (TrainBean) o;
            textTrainName.setText(trainBean.getT_name());
            textTrainPrice.setText(String.valueOf(trainBean.getT_price()));
            textTrainType.setText(String.valueOf(trainBean.getT_type()));
        }
        if (flag==2){
            GetInfo getInfo = (GetInfo) o;
            textUserName.setText(getInfo.getU_name());
            textUserPhone.setText(getInfo.getU_phone());
        }

        if (flag==5) {
            IsTrainn isTrainn = (IsTrainn) o;
            if (isTrainn.getTn_state().equals("已报名")) {
                Toast.makeText(TrainnActivity.this, "该培训已报名", Toast.LENGTH_SHORT).show();
            }
        }
        if (flag==4){
        TrainnBean trainnBean = (TrainnBean) o;
        if (trainnBean.getSuccess().equals("1")) {
            Toast.makeText(TrainnActivity.this, "报名成功", Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(TrainnActivity.this, "报名失败", Toast.LENGTH_SHORT).show();
        }
    }
}
    @Override
    public void onFail() {

    }
}
